<template>
    <div class="Login">
        <el-row :gutter="20">
            <el-col :span="8" :offset="8">
                <div class="grid-content"></div>
            </el-col>
        </el-row>


        <el-row type="flex" justify="center">
            <el-col class="el-col" :span="7">
                <el-card class="el-card" shadow="always">
                    <h1>管理员登录</h1>
                    <el-divider></el-divider>
                    <el-form :model="ValidateForm" ref="ValidateForm" label-width="100px" class="demo-ruleForm">
                        <el-form-item
                                label="用户名"
                                prop="name"
                                :rules="[
                                          { required: true, message: '用户名不能为空'},
                                        ]"
                        >
                            <el-input placeholder="请输入用户名" type="text" v-model="ValidateForm.name"
                                      autocomplete="off"></el-input>
                        </el-form-item>

                        <el-form-item
                                label="密码"
                                prop="password"
                                :rules="[
                                          { required: true, message: '密码不能为空'},
                                        ]"
                        >
                            <el-input placeholder="请输入密码" v-model="ValidateForm.password" show-password></el-input>
                        </el-form-item>

                        <div class=".el-button">
                            <el-form-item>
                                <el-button type="primary" @click="submitForm('ValidateForm')">提交</el-button>
                                <el-button @click="resetForm('ValidateForm')">重置</el-button>
                            </el-form-item>
                        </div>
                    </el-form>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                ValidateForm: {
                    name: '',
                    password: ''
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios({
                            method: 'post',
                            url: 'https://kongbai121.xyz/miniapp//admins/loginByAjax',
                            // url: 'http://localhost:8080/admins/loginByAjax',
                            data: this.qs.stringify({    //这里是发送给后台的数据
                                username: this.ValidateForm.name,
                                password: this.ValidateForm.password,
                            })
                        }).then((response) => {          //这里使用了ES6的语法
                            if (response.data.success) {
                                window.localStorage.setItem('token', response.data.msg);
                                alert("登录成功");
                                this.$router.push('/resources-save');
                            } else {
                                alert(response.data.msg);
                                this.$refs[formName].resetFields();
                            }
                        }).catch((error) => {
                            console.log(error)       //请求失败返回的数据
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }

</script>

<style scoped>
    .el-container:nth-child(5) .el-aside,
    .el-container:nth-child(6) .el-aside {
        line-height: 260px;
    }

    .el-container:nth-child(7) .el-aside {
        line-height: 320px;
    }

    .grid-content {
        border-radius: 4px;
        min-height: 150px;
    }

    .demo-ruleForm {
        width: 90%;
        position: relative;
    }

    .el-button {
        transform: translateX(-15px);
    }

    .el-col {
        width: 553px;
    }
</style>